package com.lujianfei.imagesearch.ui.subui;

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.lujianfei.imagesearch.base.BaseChildWindow;
import com.lujianfei.imagesearch.contract.CanvasContract;
import com.lujianfei.imagesearch.presenter.CanvasPresenter;
import com.lujianfei.imagesearch.widget.ImageView;
import com.lujianfei.lib.sdautolayout.impl.SDAutoLayout;

public class CanvasWindow extends BaseChildWindow implements CanvasContract.View {

	ImageView imagView = new ImageView();
	CanvasContract.Presenter presenter = new CanvasPresenter(this);
	String absolutePath;
	

	public CanvasContract.Presenter getPresenter() {
		return presenter;
	}

	public CanvasWindow() {
		super(500, 500);
		init();
	}
	
	@Override
	public void setTitle(String title) {
		// TODO Auto-generated method stub
		super.setTitle(title + " " +  absolutePath);
	}

	private void init() {
		add(imagView);
		SDAutoLayout.forComponent(imagView).widthIs(getWidth()).heightIs(getHeight());
	}
	
	@Override
	protected void onResize() {
		// TODO Auto-generated method stub
		super.onResize();
		SDAutoLayout.forComponent(imagView).widthIs(getWidth()).heightIs(getHeight());
	}
	
	@Override
	public Component getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void setFileName(String absolutePath) {
		this.absolutePath = absolutePath;
		try {
		    imagView.setImage(ImageIO.read(new File(absolutePath)));
		} catch (IOException e) {
			System.err.print(e);
		}
	}


	@Override
	public BufferedImage getImage() {
		// TODO Auto-generated method stub
		return imagView.getImage();
	}

	@Override
	public void updateImage() {
		imagView.repaint();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (null != onCloseListener) {
			onCloseListener.onClose(this);
		}
	}
}
